package com.demo.core.service.base.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.core.dao.TemplateDao;
import com.demo.core.dao.base.support.ResultSearchResult;
import com.demo.core.dao.base.support.SearchArgumentSupport;
import com.demo.core.model.Template;
import com.demo.core.service.base.TemplateService;

/**
 * 模板服务
 * 
 * @author ryuu.kk
 */
public class TemplateServiceImpl implements TemplateService {

	/**
	 * 文章扩展属性Dao
	 */
	private TemplateDao templateDao;

	@Override
	public Long save(Template template) {
		return templateDao.insert(template);
	}

	@Override
	public boolean exists(Long id) {
		return templateDao.exists(id);
	}

	@Override
	public void delete(Long id) {
		templateDao.delete(id);
	}

	@Override
	public void update(Long id, String name, String path, String content) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		Template template = new Template();
		template.setContent(content);
		template.setName(name);
		template.setPath(path);
		templateDao.update(template, params);
	}

	@Override
	public Template find(Long id) {
		return templateDao.select(id);
	}

	@Override
	public Template find(String path) {
		return find(path, true);
	}
	
	@Override
	public Template find(Long id, boolean isContent) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", id);
		params.put("isContent", Boolean.valueOf(isContent));
		List<Template> templateList = templateDao.select(params);
		return templateList.size() > 0 ? templateList.get(0) : null;
	}

	@Override
	public Template find(String path, boolean isContent) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("path", path);
		params.put("isContent", Boolean.valueOf(isContent));
		List<Template> templateList = templateDao.select(params);
		return templateList.size() > 0 ? templateList.get(0) : null;
	}
	
	@Override
	public List<Template> findList(Long siteId, boolean isContent) {
		return findList(siteId, "", isContent);
	}

	@Override
	public List<Template> findList(Long siteId, String type, boolean isContent) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		params.put("type", type);
		params.put("isContent", Boolean.valueOf(isContent));
		return templateDao.select(params);
	}

	@Override
	public ResultSearchResult<Template> search(Long siteId, String type, boolean isContent, SearchArgumentSupport sas) {
		Map<String, Object> params = new HashMap<String, Object>(7);
		params.put("siteId", siteId);
		params.put("type", type);
		params.put("isContent", Boolean.valueOf(isContent));
		// SearchArgumentSupport sas = new SearchArgumentSupport(firstResult, maxResults);
		return templateDao.search(params, sas);
	}
	
	@Override
	public ResultSearchResult<Template> search(Long siteId, boolean isContent, SearchArgumentSupport sas) {
		return search(siteId, "", isContent, sas);
	}
	
	public void setTemplateDao(TemplateDao templateDao) {
		this.templateDao = templateDao;
	}
}
